package edu.indiana.d2i.vmm.job;

import java.io.ByteArrayInputStream;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;

import edu.indiana.d2i.vmm.job.rsl.JobDescriptionType;
import edu.indiana.d2i.vmm.job.rsl.NameValuePairType;

public class SigiriHTRCJobRequest {
	public String userID;

	public String executablePath;
	public String executableName;
	public String propName;
	public String propPath;
	public String volumeIdsPath;
	public String jobType;
	public String imageType;
	public int instanceNum;
	public String softwareFrameWork; // only one framework

	public String outputFileName;
	public String temporaryFileName;
	public String[] args;
	public String exeCmdLine;

	private static class RSLValidationEventHandler implements
			ValidationEventHandler {
		public boolean handleEvent(ValidationEvent ve) {
			if (ve.getSeverity() == ValidationEvent.FATAL_ERROR
					|| ve.getSeverity() == ValidationEvent.ERROR)
				return false;
			return true;
		}
	}

	@SuppressWarnings("unchecked")
	public static SigiriHTRCJobRequest parseFromXML(String xmlString)
			throws JAXBException {
		SigiriHTRCJobRequest request = new SigiriHTRCJobRequest();
		JAXBContext jaxbContext = JAXBContext
				.newInstance("edu.indiana.d2i.vmm.job.rsl");
		Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
		unmarshaller.setEventHandler(new RSLValidationEventHandler());

		JAXBElement<JobDescriptionType> job = (JAXBElement<JobDescriptionType>) unmarshaller
				.unmarshal(new ByteArrayInputStream(xmlString.getBytes()));
		JobDescriptionType description = job.getValue();

		List<NameValuePairType> environment = description.getEnvironment();
		for (NameValuePairType env : environment) {
			if (env.getName().equals(SigiriHTRCConstants.SOFTWARE_FRAMEWORK))
				request.softwareFrameWork = env.getValue();
			else if (env.getName().equals(SigiriHTRCConstants.IMAGE_TYPE))
				request.imageType = env.getValue();
			else if (env.getName().equals(
					SigiriHTRCConstants.INSTANCE_NUMBER))
				request.instanceNum = Integer.valueOf(env.getValue());
			else if (env.getName().equals(SigiriHTRCConstants.EXECUTABLE_PATH))
				request.executablePath = env.getValue();
			else if (env.getName().equals(SigiriHTRCConstants.OUTPUT_FILE))
				request.outputFileName = env.getValue();
			else if (env.getName()
					.equals(SigiriHTRCConstants.TEMPORARY_FILE))
				request.temporaryFileName = env.getValue();
			else if (env.getName()
					.equals(SigiriHTRCConstants.PROPERTY_PATH))
				request.propPath = env.getValue();
			else if (env.getName()
					.equals(SigiriHTRCConstants.PROPERTY_NAME))
				request.propName = env.getValue();
		}
		request.args = (String[]) description.getArgument().toArray(
				new String[description.getArgument().size()]);

		request.userID = description.getLocalUserId();
		request.jobType = description.getJobType().value();
		request.executableName = description.getExecutable();
		StringBuilder cmd = new StringBuilder();
		cmd.append(request.executableName + " ");
		request.args = description.getArgument().toArray(
				new String[description.getArgument().size()]);
		for (String parameter : request.args) cmd.append(parameter + " ");
		request.exeCmdLine = cmd.toString();
		
		return request;
	}
}
